//
//  SCPhotoSubCell.m
//  PhotoAlbum
//
//  Created by Chao Sheng on 2021/12/8.
//

#import "SCPhotoSubCell.h"
#import "SCHitButton.h"
#import "SCCircleLoadProgressView.h"

@implementation SCPhotoAddCell

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    UIImageView *imageView = [[UIImageView alloc] init];
    [self addSubview:imageView];
   
    imageView.image = [UIImage sc_bundleImageNamed:@"LGQ-add"];
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsMake(5, 5, 5, 5));
    }];
}

@end

@interface SCPhotoSubCell ()

@property (nonatomic, strong) SCHitButton *deleteBtn;

@property (strong, nonatomic) SCCircleLoadProgressView *progressView;

@end

@implementation SCPhotoSubCell

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    [self.contentView addSubview:self.imageView];
    [self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsMake(5, 5, 5, 5));
    }];
    [self.contentView addSubview:self.deleteBtn];
    [self.deleteBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.equalTo(@(18));
        make.right.equalTo(@-2.5);
        make.top.equalTo(@2.5);
    }];
    
    [self.contentView addSubview:self.progressView];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    self.progressView.center = self.contentView.center;
}

- (void)setPhotoModel:(SCPhotoModel *)photoModel {
    _photoModel = photoModel;
    sc_weak(self);
    if (_photoModel.highQualityImage) {
        self.imageView.image = self.photoModel.highQualityImage;
        
    } else if (_photoModel.asset) {
        
        [SCPhotoTools getPhotoForPHAsset:_photoModel.asset size:CGSizeMake(self.photoModel.imgSize.width, self.photoModel.imgSize.height) completion:^(UIImage * _Nonnull image, PHAsset *asset) {
            if (asset == weakself.photoModel.asset) {
                weakself.imageView.image = image;
                weakself.photoModel.highQualityImage = image;
                weakself.photoModel.tempImage = image;
            }
        }];
        
    } else if (_photoModel.imageNetworkingUrl) {
        //加载网络图片
        self.progressView.hidden = NO;
        CGFloat progress = 0;
        self.progressView.progress = progress;
        [self.imageView sc_setImageWithModel:_photoModel progress:^(CGFloat progress, SCPhotoModel * _Nonnull model) {
            if (model==weakself.photoModel) {
                NSLog(@"进度=%.1f",progress);
                weakself.progressView.progress = progress;
            }
        } completed:^(UIImage * _Nonnull image, NSError * _Nonnull error, SCPhotoModel * _Nonnull model) {
            if (model==weakself.photoModel) {
                if (!error) {
                    if (image) {
                        weakself.progressView.progress = 1;
                        weakself.progressView.hidden = YES;
                        weakself.imageView.image = image;
                    }
                } else {
                    [weakself.progressView showError];
                }
            }
        }];
    }
}

- (void)setManager:(SCPhotoManager *)manager {
    _manager = manager;
    if (_manager.editEnble) {
        self.deleteBtn.hidden = NO;
    } else {
        self.deleteBtn.hidden = YES;
    }
}

- (void)deleteAction {
    
    if (self.scDeletePhoto) {
        self.scDeletePhoto(self.photoModel);
    }
}

#pragma mark - 懒加载
- (UIImageView *)imageView {
    if (!_imageView) {
        _imageView = [[UIImageView alloc] init];
        _imageView.contentMode = UIViewContentModeScaleAspectFill;
        _imageView.layer.cornerRadius = 8;
        _imageView.layer.masksToBounds = true;
    }
    return _imageView;
}

- (SCHitButton *)deleteBtn {
    if (!_deleteBtn) {
        _deleteBtn = [SCHitButton buttonWithType:UIButtonTypeCustom];
        [_deleteBtn setImage:[UIImage sc_bundleImageNamed:@"LGQ-cancel"] forState:UIControlStateNormal];
        _deleteBtn.backgroundColor = [UIColor whiteColor];
        _deleteBtn.layer.cornerRadius = 9;
        _deleteBtn.layer.masksToBounds = true;
        [_deleteBtn addTarget:self action:@selector(deleteAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _deleteBtn;
}

- (SCCircleLoadProgressView *)progressView {
    if (!_progressView) {
        _progressView = [[SCCircleLoadProgressView alloc] init];
        _progressView.hidden = YES;
    }
    return _progressView;
}

@end
